/*
 * Copyright 2009 SHOP.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shop.opensocial.mediated.shindig.util;

import com.shop.opensocial.mediated.services.ContainerScriptsAndConstantsMediator;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * <br>
 * (c)2009 by SHOP.COM<br>
 * All rights reserved worldwide<br>
 *
 * @author Jordan Zimmerman
 */
public class ContainerScriptsAndConstantsMediatorImplementation implements ContainerScriptsAndConstantsMediator
{
	public static final ContainerScriptsAndConstantsMediatorImplementation instance = new ContainerScriptsAndConstantsMediatorImplementation();

	@Override
	public String getSecurityTokenFromURL(HttpServletRequest request)
	{
		return request.getParameter(SECURITY_TOKEN_PARAMETER_NAME_STR);
	}

	@Override
	public String makeGadgetIFrameURL(String iframeHostname, String parentHostname, boolean ssl, String securityTokenValue)
	{
		try
		{
			StringBuilder		url = new StringBuilder(ssl ? "https://" : "http://");
			url.append(iframeHostname).append(GADGETS_SERVLET_PATH).append('?');
			url.append("parent").append('=').append(URLEncoder.encode((ssl ? "https://" : "http://") + parentHostname, "UTF-8")).append('&');
			url.append(SECURITY_TOKEN_PARAMETER_NAME_STR).append('=').append(URLEncoder.encode(securityTokenValue, "UTF-8"));
			url.append("#").append(System.nanoTime());
			return url.toString();
		}
		catch ( UnsupportedEncodingException e )
		{
			throw new Error(e);	// will never get here
		}
	}

	@Override
	public String getContainerIncludes(String hostname, boolean ssl)
	{
		StringBuilder 		url = new StringBuilder(ssl ? "https://" : "http://");
		url.append(hostname);
		url.append("/gadgets/js/rpc.js");
		return "<script src=\"" + url + "\"></script>\n";
	}

	@Override
	public String getContainerRegistration(RegistrationTypes type, String callbackFunctionName)
	{
		String		internalName = "";
		String		arguments = "";
		switch ( type )
		{
			case SET_TITLE:
			{
				internalName = "set_title";
				arguments = "title";
				break;
			}

			case REQUEST_NAVIGATE_TO:
			{
				internalName = "requestNavigateTo";
				arguments = "view, opt_params";
				break;
			}

			case DYNAMIC_HEIGHT:
			{
				internalName = "resize_iframe";
				arguments = "height";
				break;
			}
		}

		return fRegistrationScript.replaceAll("\\$internalName", internalName).
			replaceAll("\\$arguments", arguments).
			replaceAll("\\$callbackFunctionName", callbackFunctionName);
	}

	private ContainerScriptsAndConstantsMediatorImplementation()
	{
	}

	private static final String		fRegistrationScript =
		"<script language=\"javascript\">\n" +
			"gadgets.rpc.register\n" +
			"(\n" +
			"\t\"$internalName\", \n" +
			"\tfunction($arguments)\n" +
			"\t{\n" +
			"\t\tvar applicationId = this.f;\n" +
			"\t\t$callbackFunctionName(applicationId, $arguments);\n" +
			"\t}\n" +
			");\n" +
			"</script>\n";

	private static final String GADGETS_SERVLET_PATH = "/gadgets/ifr";
	private static final String SECURITY_TOKEN_PARAMETER_NAME_STR = "st";
}
